<?php

class Import extends MY_Controller {
	var $type; // bill, order, etc
	var $subtype; // default: equal type
	var $data;
	
	function __construct()
	{
		parent::MY_Controller();
		
		$this->load->model('Shop');
		$this->load->library('parser');
		$this->load->library('table');
		
		$this->template->enable_parser(TRUE);
		$this->type 	= $this->uri->segment(2, 0);
		$this->subtype 	= $this->uri->segment(3, 0);
		
		//$input['page'] = $this->input->get('page', TRUE);
		//$input['action'] = $this->input->get('action', TRUE);
		
		// data head
		$this->data["head"]['heading'] = "NAVIGATION";
		$this->data["head"]['menu_links'] = array(
			array('name'=>'Trang chu import', 'url'=>'/'),
			array('name'=>'Quan ly hoa don mua hang', 'url'=>'bill'),
			array('name'=>'Quan ly hang hoa', 'url'=>'product')
		);
		
		// data body
		$this->data["body"]["path"] = $this->config->item("base_url");
		
		// load view
		if (!$this->subtype) 
			$this->subtype = $this->type;
					
		if (strlen($this->type)>0 && file_exists(APPPATH. 'views/forms/'. $this->type. '/'. $this->subtype. '.php')) {
			switch ($this->type){
				case "bill":
					//$query = $this->Shop->get_suppliers(); 
					/*
					foreach ($query->result() as $row){
						print_r($row). "<br>";
					}
					*/
					$form['heading'] = "Nhap hoa don mua hang";
					$form['suppliers'] = $this->Shop->get_suppliers();
					
					//$this->load->library('table');
					$query = $this->db->query("SELECT * FROM ps_bill_detail");
					echo $this->table->generate($query); 
					
					// draw bill detail
					// $data = array(
						// array('Name', 'Color', 'Size'),
						// array('Fred', 'Blue', 'Small'),
						// array('Mary', 'Red', 'Large'),
						// array('John', 'Green', 'Medium')
					// );
					// echo $this->table->generate($data); 
					
					// 
					//$query = $this->Shop->get_suppliers();
					//$this->table->generate($query);
					
					// get a bill detail
					// $bill = $this->Shop->get_bill(1);
					// print_r($bill);
					
					// get bill list
					//$bills = $this->Shop->get_bill_list(10, 0);
					//print_r($bills);
					
					// insert a bill
					
					// insert bill detail
					// $ins_data = array(
						// 'id_bill' 		=> 1,
						// 'product_name' 	=> 'test',
						// 'gender' 		=> 'M',
						// 'quantity' 		=> 100,
						// 'price_each' 	=> 230,
						// 'description' 	=> 'test'
					// );
					// $this->Shop->insert_bill_detail($ins_data);
					
					$title = "Nhap hoa don mua hang";
					break;
				case "order":
					$title = "Nhap hoa don ban hang";
					break;
				case "product":
					$title = "Nhap hang hoa";
					break;
				default:
					break;
			}
			$this->data["head"]["title"] = $title;
			$this->data["body"]["content"] = $this->load->view("forms/". $this->type. "/". $this->subtype, $form, true);
		} else {
			$this->data["head"]["title"] = "Page not found";
			$this->data["body"]["content"] = "Page not found";
		}
	}
	
	function _show($data) {
		$this->template->set_partial('header', 'partials/header', $data["head"]);
		$this->template->title($data["head"]["title"]);
		$this->template->build('body', $data["body"]);
	}
	
	function index() {
		$this->_show($this->data);
	}
	
	function bill() {		
		$this->_show($this->data);
	}
	
	function product() {
		$this->_show($this->data);
	}
}